﻿/* A control used to show a menu with ambiguous geocoding results
 */

MapControl2.AAAGeoCodingMenu = function(inputControl, menuLR, menuUD) {
    
    if (menuLR == "undefined" || (menuLR != MapControl2.AAAGeoCodingMenu.LEFT && menuLR != MapControl2.AAAGeoCodingMenu.RIGHT)) {
        menuLR = MapControl2.AAAGeoCodingMenu.LEFT;
    }
    if (menuUD == "undefined" || (menuUD != MapControl2.AAAGeoCodingMenu.UP && menuUD != MapControl2.AAAGeoCodingMenu.DOWN)) {
        menuUD = MapControl2.AAAGeoCodingMenu.DOWN;
    }
    
    createChooseLocation();
    
    inputControl.geoCode = function() {
        if (inputControl.value != "" && inputControl.enabledGeocoding == true) {
            var netUtils = new MapControl2.AAANetUtils();
            netUtils.performRequest(MapControl2.AAANetUtils.GEOCODING_REQUEST, '"'+inputControl.value+'"', inputControl.geoCodingOk, inputControl.geoCodingFail);
        }
    }
    
    inputControl.enableGeocoding = function(value) {
        if (value == true || value == false) {
            inputControl.enabledGeocoding = value;
        }
    }
        
    inputControl.onblur = inputControl.geoCode;
    inputControl.onchange = function() {
        inputControl.enableGeocoding(true);
    }
    inputControl.onkeydown = function() {
        if (inputControl.menuStatus == 1) {
            inputControl.hideMenu();
        }
    }
    
    inputControl.geoCodingOk = function(result) {
        chooseLocation = document.getElementById(inputControl.id + "_chLoc");
        if (result && result != null) {
            if (result.length > 1) {
                var listBox = chooseLocation.firstChild;
                inputControl.resetListBox();
                for (i = 0; i < result.length; i++) {
                    inputControl.addListItem(result[i]);
                }                   
                inputControl.showMenu();
            } else if (result.length == 1) {
                inputControl.value = result[0].Address;
                inputControl.latitude = result[0].Lat;
                inputControl.longitude = result[0].Lng;
                inputControl.enableGeocoding(false);
            } else {
                inputControl.value = "Impossibile trovare il luogo specificato";
            }
        }
    }
    
    inputControl.geoCodingFail = function() {
        alert("Impossibile trovare il luogo specificato");
    }
    
    inputControl.resetListBox = function() {
        var listBox = document.getElementById(inputControl.id + "_chLoc_ListBox");
        if (listBox.hasChildNodes()) {
            while (listBox.childNodes.length >= 1) {
                listBox.removeChild(listBox.firstChild);
            }
        }
    }
    
    inputControl.addListItem = function(geoloc) {
        var listBox = document.getElementById(inputControl.id + "_chLoc_ListBox");
        var listItem = document.createElement("li");
        listItem.innerHTML = geoloc.Address;
        listItem.latitude = geoloc.Lat;
        listItem.longitude = geoloc.Lng;
        listItem.style.padding = "2px";
        listItem.style.cursor = "pointer";
        listItem.onclick = function() {
            inputControl.value = listItem.innerHTML;
            inputControl.latitude = listItem.latitude;
            inputControl.longitude = listItem.longitude;
            inputControl.enableGeocoding(false);
            inputControl.hideMenu();
        }
        listItem.onmouseover = function() {
            listItem.style.background = "#187418";
            listItem.style.color = "#DEF17E";
        }
        listItem.onmouseout = function() {
            listItem.style.background = "inherit";
            listItem.style.color = "inherit";
        }
        listBox.appendChild(listItem);
    }
    
    inputControl.showMenu = function() {
        inputControl.setHorizontalPos(menuLR);
        inputControl.setVerticalPos(menuUD);
        var menuBox = document.getElementById(inputControl.id + "_chLoc");
        menuBox.style.top = menuBox.visibleY + "px";
        menuBox.style.left = menuBox.visibleX + "px";
        //menuBox.style.display = "block";
        inputControl.menuStatus = 1;
    }
    
    inputControl.hideMenu = function() {
        var menuBox = document.getElementById(inputControl.id + "_chLoc");
        menuBox.style.top = "-10000px";
        menuBox.style.left = "-10000px";
        //menuBox.style.display = "none";
        inputControl.menuStatus = 0;
    }
    
    inputControl.setHorizontalPos = function(side) {
        if (side != "undefined" && (side == MapControl2.AAAGeoCodingMenu.LEFT || side == MapControl2.AAAGeoCodingMenu.RIGHT)) {
            var container = document.getElementById(inputControl.id + "_chLoc");
            var inputPosition = MapControl2.AAAUtils.elementPosition(inputControl, false);
            if (menuLR == MapControl2.AAAGeoCodingMenu.LEFT) {
                container.visibleX = inputPosition.x + inputControl.clientWidth + 4;
            } else {
                container.visibleX = inputPosition.x - container.clientWidth;
            }
        }
    }
    
    inputControl.setVerticalPos = function(dir) {
        if (dir != "undefined" && (dir == MapControl2.AAAGeoCodingMenu.UP || dir == MapControl2.AAAGeoCodingMenu.DOWN)) {
            var container = document.getElementById(inputControl.id + "_chLoc");
            var inputPosition = MapControl2.AAAUtils.elementPosition(inputControl, false);
            if (menuUD == MapControl2.AAAGeoCodingMenu.DOWN) {
                container.visibleY = inputPosition.y + 2;
            } else {
                container.visibleY = inputPosition.y - container.clientHeight + inputControl.clientHeight + 4;
            }
        }
    }
    
    function createChooseLocation() {
        var container = document.createElement("div");
        container.id = inputControl.id + "_chLoc";
        container.style.width = "auto";
        container.style.height = "auto";
        container.style.backgroundColor = "#DEF17E";
        container.style.position = "absolute";
        container.style.top = "-10000px";
        container.style.left = "-10000px";
        container.style.textAlign = "left";
        //container.style.display = "none";
        container.style.fontFamily = "Verdana,Arial,Helvetica,sans-serif";
        container.style.fontSize = "0.75em";
        container.style.fontWeight = "bold";
        container.style.color = "#187418";
        container.status = 0;
        
        var listBox = document.createElement("ul");
        listBox.id = inputControl.id + "_chLoc_ListBox";
        listBox.style.listStyleType = "none";
        listBox.style.position = "relative";
        listBox.style.top = "0";
        listBox.style.left = "0";
        listBox.style.padding = "0 2px";
        container.appendChild(listBox);
        
        if (inputControl.nextSibling) {
            inputControl.parentNode.insertBefore(container, inputControl.nextSibling);
        } else {
            inputControl.parentNode.appendChild(container);
        }
    }
    
}

MapControl2.AAAGeoCodingMenu.LEFT = 0;
MapControl2.AAAGeoCodingMenu.RIGHT = 1;
MapControl2.AAAGeoCodingMenu.UP = 0;
MapControl2.AAAGeoCodingMenu.DOWN = 1;